package entities;

import view.ChessBoard;

import java.awt.*;
import java.util.List;
import java.util.Map;
import java.util.Set;

/***
 * Author: Mr.Zheng, Time: 2018/5/6/7：46
 * Description: 馬
 */
public class Ma extends BasePiece {
    public Ma(int x, int y, Color color) {
        super(x, y, color);
        this.pieceName = "馬";
    }


    @Override
    public void updateAvailablePoints(Map<Point, BasePiece> pointPieceMap) {

        availablePoints.clear();
        int x = getPosition().x;
        int y = getPosition().y;

//首先获取马上方三个点的坐标（第三个是蹩马腿坐标）
        BasePiece piece1 = pointPieceMap.get(new Point(x - 1, y - 2));
        BasePiece piece2 = pointPieceMap.get(new Point(x + 1, y - 2));
        BasePiece piece3 = pointPieceMap.get(new Point(x, y - 1));
        /*
        先判断蹩马腿的地方是否有棋子，
        没有棋子后进行两个可移动点判断
        */
        if (piece3 == null) {
            //当点为空或者为敌方棋子时，添加该点
            if (piece1 == null || piece1.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x - 1, y - 2));
            }
            if (piece2 == null || piece2.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x + 1, y - 2));
            }
        }
//首先获取马下方三个点的坐标（第三个是蹩马腿坐标）
        BasePiece piece4 = pointPieceMap.get(new Point(x - 1, y + 2));
        BasePiece piece5 = pointPieceMap.get(new Point(x + 1, y + 2));
        BasePiece piece6 = pointPieceMap.get(new Point(x, y + 1));
        /*
        先判断蹩马腿的地方是否有棋子，
        没有棋子后进行两个可移动点判断
        */
        if (piece6 == null) {
            //当点为空或者为敌方棋子时，添加该点
            if (piece4 == null || piece4.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x - 1, y + 2));
            }
            if (piece5 == null || piece5.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x + 1, y + 2));
            }
        }
//首先获取马左方三个点的坐标（第三个是蹩马腿坐标）
        BasePiece piece7 = pointPieceMap.get(new Point(x - 2, y - 1));
        BasePiece piece8 = pointPieceMap.get(new Point(x - 2, y + 1));
        BasePiece piece9 = pointPieceMap.get(new Point(x - 1, y));
        /*
        先判断蹩马腿的地方是否有棋子，
        没有棋子后进行两个可移动点判断
        */
        if (piece9 == null) {
            //当点为空或者为敌方棋子时，添加该点
            if (piece7 == null || piece7.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x - 2, y - 1));
            }
            if (piece8 == null || piece8.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x - 2, y + 1));
            }
        }
        //首先获取马右方三个点的坐标（第三个是蹩马腿坐标）
        BasePiece piece10 = pointPieceMap.get(new Point(x + 2, y - 1));
        BasePiece piece11 = pointPieceMap.get(new Point(x + 2, y + 1));
        BasePiece piece12 = pointPieceMap.get(new Point(x + 1, y));
        /*
        先判断蹩马腿的地方是否有棋子，
        没有棋子后进行两个可移动点判断
        */
        if (piece12 == null) {
            //当点为空或者为敌方棋子时，添加该点
            if (piece10 == null || piece10.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x + 2, y - 1));
            }
            if (piece11 == null || piece11.getColor() != this.getColor()) {
                this.availablePoints.add(new AvailablePoint(x + 2, y + 1));
            }
        }
    }
}
